package svc

import (
	"github.com/zeromicro/go-zero/core/bloom"
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"gocode/goland/shortener/internal/config"
	"gocode/goland/shortener/model"
	"gocode/goland/shortener/sequence"
)

type ServiceContext struct {
	Config            config.Config
	ShortUrlModel     model.ShortUrlMapModel
	Sequence          sequence.Sequence
	ShortUrlBlackList map[string]struct{}
	//布隆过滤器 go-zero自带
	Fliter *bloom.Filter
}

func NewServiceContext(c config.Config) *ServiceContext {
	conn := sqlx.NewMysql(c.ShortUrlDB.DSN)
	m := make(map[string]struct{}, len(c.ShortUrlBlackList))
	for _, v := range c.ShortUrlBlackList {
		m[v] = struct{}{}
	}
	//初始化布隆过滤器
	store := redis.New(c.CacheRedis[0].Host, func(r *redis.Redis) {
		r.Type = redis.NodeType // 设置为节点模式
	})
	fliter := bloom.New(store, "bloom_filter", 20*(1<<20))
	//加载已有的短链接数据
	return &ServiceContext{
		Config:        c,
		ShortUrlModel: model.NewShortUrlMapModel(conn, c.CacheRedis),
		//Sequence:      sequence.NewMysql(c.Sequence.DSN),
		Sequence:          sequence.NewRedis(c.CacheRedis),
		ShortUrlBlackList: m,
		Fliter:            fliter,
	}
}

//loadDataToBloom 加载已有的数据去布隆过滤器
func loadDataToBloom() {

}
